package org.visionlibrary.image.model;

import java.awt.Point;

import javax.media.jai.TiledImage;

public class KernelFilter extends WindowFilter {
	ConvolutionKernel kernel = null;

	final protected int getNewPixelVal(TiledImage src, Point p, int channel)
			throws NullPointerException {
		if (kernel == null)
			throw new NullPointerException(
					"Convolution kernel is null.");

		float total = 0;
		Point pIter = new Point();
		for (pIter.y = 0; pIter.y < windowHeight; pIter.y++)
			for (pIter.x = 0; pIter.x < windowWidth; pIter.x++) {
				total += getWindowPixelVal(src, pIter, channel)
						* kernel.getValue(pIter);
			}

		return (int) (total / kernel.getDivisor());
	}

	final protected void setWindowProperties() {
		this.windowHeight = kernel.getHeight();
		this.windowWidth = kernel.getWidth();
		this.dstPointInWindow = kernel.getOrigin();
	}

	public ConvolutionKernel getKernel() {
		return kernel;
	}

	public void setKernel(ConvolutionKernel kernel) {
		this.kernel = kernel;
	}
}
